package com.slipper.filter;

import com.slipper.common.bo.UserDetail;
import com.slipper.constants.Constants;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class HttpRequestActuatorFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        try {
            String path = httpRequest.getServletPath();
            if (path.startsWith("/actuator")) {
                UserDetail userDetail = Constants.getSessionUser(httpRequest.getSession());
                if (userDetail == null || !userDetail.isSuperAdmin()) {
                    httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
            }

            filterChain.doFilter(httpRequest, response);
        } catch (Exception e) {
            httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}
